Skip to content

fix(rivetkit-native): expose full hibernation metadata to JS#4657

Draft
NathanFlurry wants to merge 1 commit intobreak-up/persist-hibernating-requestsfrom
break-up/expose-hibernation-metadata
Draft

fix(rivetkit-native): expose full hibernation metadata to JS#4657
NathanFlurry wants to merge 1 commit intobreak-up/persist-hibernating-requestsfrom
break-up/expose-hibernation-metadata

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 14, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude bot commented Apr 14, 2026

PR Review: fix(rivetkit-native): expose full hibernation metadata to JS

Overview

This PR does two related things:

  1. Forwards the previously zeroed-out envoyMessageIndex, rivetMessageIndex, path, and headers fields from JS hibernation restore entries into the native HibernatingWebSocketMetadata struct.
  2. Implements the can_hibernate callback, which was previously a stub returning false. It now calls a JS canHibernate handler synchronously using block_in_place + a std::sync::Mutex-backed response map.

The first change is straightforward and correct. The second is more complex and has some concerns.


Issues

Critical: Indefinite block when JS does not call respondCallback

can_hibernate blocks on rx.blocking_recv() with no timeout. In the JS handler, respondCallback is only called if handle._raw is truthy:

async (canHibernate) => {
    if (handle._raw) {  // if null, Rust blocks forever
        await handle._raw.respondCallback(...);
    }
},

If handle._raw is null at call time (e.g. handle being torn down), the Rust thread blocks indefinitely and stalls the envoy thread pool. A timeout on the rx side (e.g. tokio::time::timeout) would make this safe.

Potential memory leak when response is never sent

tx is inserted into response_map and only removed by respondCallback via map.remove(&response_id). If respondCallback is never called (the if handle._raw guard makes this possible), the entry leaks indefinitely. The Rust side should clean up the map entry after blocking_recv returns, regardless of outcome.

ThreadsafeFunctionCallMode::Blocking vs NonBlocking

All other event_cb.call sites use NonBlocking. The new can_hibernate uses Blocking, which will stall the calling thread if the TSFN queue is full. Given that can_hibernate already blocks on block_in_place this is tolerable, but the difference from every other call site is worth a comment explaining the intent.


Minor Issues

unwrap_or_else(HashMap::new) should be unwrap_or_default() (envoy_handle.rs)

error field in JS response is silently ignored by Rust. The error path sends { canHibernate: false, error: String(err) } but Rust only reads canHibernate. Consider a tracing::warn! on the Rust side for the error case.

URL construction assumes path starts with /. new Request(\http://actor${event.path}\`, ...)produceshttp://actorfoo` if path is empty or lacks a leading slash. A fallback like event.path || '/' would be safer.


What looks good

  • The std::sync::Mutex switch for ResponseMap is correct. can_hibernate is synchronous so tokio::sync::Mutex cannot be used there. All async callbacks hold the lock only briefly before dropping it, never across .await points.
  • block_in_place is the correct primitive for blocking inside a Tokio multi-thread runtime.
  • Forwarding actual metadata fields instead of hardcoded zeros/empty strings is clearly the right fix.
  • TypeScript types in index.d.ts correctly reflect the new fields (number for u16, optional Record<string,string> for Option<HashMap<String,String>>).
  • The ?? 0 / ?? "" / ?? {} defaults in wrapper.js are consistent with unwrap_or_default() on the Rust side.

@NathanFlurry NathanFlurry force-pushed the break-up/expose-hibernation-metadata branch from 26f98bc to fde1e0b Compare April 15, 2026 02:40
@NathanFlurry NathanFlurry force-pushed the break-up/persist-hibernating-requests branch from 88f4613 to ae4fe54 Compare April 15, 2026 02:50
@NathanFlurry NathanFlurry force-pushed the break-up/expose-hibernation-metadata branch from fde1e0b to 789b9cd Compare April 15, 2026 02:50
@NathanFlurry NathanFlurry force-pushed the break-up/persist-hibernating-requests branch from ae4fe54 to c862fec Compare April 15, 2026 06:55
@NathanFlurry NathanFlurry force-pushed the break-up/expose-hibernation-metadata branch from 789b9cd to 38e331e Compare April 15, 2026 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant